const express = require('express')
const multer = require('multer')
const router = express.Router()
const fs = require('fs')
const pool = require('./pool');
const path = require('path')
//注册
router.post('/up', function(req, res) {
	var json = req.body
	console.log(json)
	pool.mypool({
		arr: [json.user],
		sql: 'select user from login where user=?',
		success(data) {
			if(data.length) {
				res.send('账号已存在')
			} else {
				pool.mypool({
					arr: [json.user, json.pass, json.name, json.img],
					sql: 'insert into login(user,pass,name,img) values(?,?,?,?)',
					success(data) {
						res.send('注册成功')
					},
					error(err) {
						res.send(err)
					}
				})
			}
		},
		error(err) {
			res.send(err)
		}
	})
})
//登录
router.post('/in', function(req, res) {
	var json = req.body
	pool.mypool({
		arr: [json.user, json.pass],
		sql: 'select * from login where user=? and pass=?',
		success(data) {
			if(data.length) {
				data[0].pass = ''
				res.send(data[0])
			} else {
				res.send('账号或密码错误')
			}
		},
		error(err) {
			res.send(err)
		}

	})
})
//点击率最高
router.post('/hot', function(req, res) {
	pool.mypool({
		sql: 'select * from search',
		arr: [],
		success(data) {
			res.send(data)
		},
		error(err) {
			res.send(err)
		}
	})
});
//小说详情
router.post('/xiang', function(req, res) {
	var loginuid = req.body.loginuid
	pool.mypool({
		sql: 'select * from search where uid=?',
		arr: [loginuid],
		success(data) {
			res.send(data)
		},
		error(err) {
			res.send(err)
		}
	})
});
//小说列表
router.post('/list', function(req, res) {
	var searchuid = req.body.searchuid
	pool.mypool({
		sql: 'select * from list where searchuid=?',
		arr: [searchuid],
		success(data) {
			res.send(data)
		},
		error(err) {
			res.send(err)
		}
	})
});
//小说内容
router.post('/cont', function(req, res) {
	var str = req.body.cont
	var uid = req.body.uid
	console.log(str)
	console.log(str, uid)
	pool.mypool({
		sql: 'select * from list where booklist=? and searchuid=?',
		arr: [str, uid],
		success(data) {
			res.send(data)
		},
		error(err) {
			res.send(err)
		}
	})
});
//小说章节修改
router.post('/xiu', function(req, res) {
	var list = req.body.blist
	var uid = req.body.bsuid
	var cont = req.body.btext
	pool.mypool({
		sql: 'update list set text=? where booklist=? and searchuid=?',
		arr: [cont, list, uid],
		success(data) {
			res.send(data)
		},
		error(err) {
			res.send(err)
		}
	})
});
//小说章节删除
router.post('/del', function(req, res) {
	var list = req.body.blist
	var uid = req.body.suid
	pool.mypool({
		sql: 'delete from list where booklist=? and searchuid=?',
		arr: [list, uid],
		success(data) {
			res.send('ok')
		},
		error(err) {
			res.send(err)
		}
	})
});
//搜索小说
router.post('/sou', function(req, res) {
	var name = req.body.names
	pool.mypool({
		sql:'select * from search where novelname like "%'+name+'%"',
		success(data) {
			if (data.length) {
				res.send(data)
			} else{
				res.send('没有找到这本小说')
			}
		},
		error(err) {
			res.send(err)
		}
	})
});
//我的小说
router.post('/bookrack', function(req, res) {
	var loginuid = req.body.loginuid
	//	console.log(typeof(name))
	pool.mypool({
		sql: 'select * from search where loginuid=? ',
		arr: [loginuid],
		success(data) {
			res.send(data)
		},
		error(err) {
			res.send(err)
		}
	})
});
//用户点击过的小说
router.post('/tj', function(req, res) {
	var loginuid = req.body.loginuid
	var suid = req.body.suid
	//	console.log(typeof(name))
	pool.mypool({
		sql: 'update search set loginuid=? where uid=?',
		arr: [loginuid, suid],
		success(data) {
			res.send(data)
		},
		error(err) {
			res.send(err)
		}
	})
});
//小说创建
router.post('/novellu', function(req, res) {
	console.log(req.body)
	var loginuid = req.body.lu
	var novelname = req.body.mz
	var author = req.body.Zz
	var novelimg = req.body.fm
	var novelsyn = req.body.Jj
	//	console.log(typeof(name))
	pool.mypool({
		sql: 'insert into search(novelname,author,novelimg,novelsyn,numbers,loginuid) values(?,?,?,?,?,?)',
		arr: [novelname, author, novelimg, novelsyn, 0, loginuid],
		success(data) {
			res.send(data)
		},
		error(err) {
			res.send(err)
		}
	})
});
//小说追加
router.post('/zhj', function(req, res) {
	var novelname = req.body.mz
	pool.mypool({
		sql: 'select * from search where novelname=?',
		arr: [novelname],
		success(data) {
			res.send(data)
		},
		error(err) {
			res.send(err)
		}
	})
});
//小说章节创建
router.post('/novellist', function(req, res) {
	console.log(req.body)
	var booklist = req.body.bookl
	var searchuid = req.body.searchuid
	var cont = req.body.cont
	//	console.log(typeof(name))
	pool.mypool({
		sql: 'insert into list(booklist,searchuid,text) values(?,?,?)',
		arr: [booklist, searchuid, cont],
		success(data) {
			res.send('ok')
		},
		error(err) {
			res.send(err)
		}
	})
});
//创建评论
router.post('/cpl', function(req, res) {
	console.log(req.body)
	var loginuser = req.body.loginuser
	var loginimg = req.body.loginimg
	var loginuid = req.body.loginuid
	var searchuid = req.body.searchuid
	var cont = req.body.cont
	pool.mypool({
		sql: 'insert into comment(loginuser,loginimg,loginuid,seachuid,text) values(?,?,?,?,?)',
		arr: [loginuser, loginimg, loginuid, searchuid, cont],
		success(data) {
			res.send('ok')
		},
		error(err) {
			res.send(err)
		}
	})
});
//追加创建的评论
router.post('/zpl', function(req, res) {
	var cont = req.body.cont
	pool.mypool({
		sql: 'select * from comment where text=? ',
		arr: [cont],
		success(data) {
			res.send(data)
		},
		error(err) {
			res.send(err)
		}
	})
});
//追加评论
router.post('/spl', function(req, res) {
	var loginuid = req.body.loginuid
	var searchuid = req.body.searchuid
	pool.mypool({
		sql: 'select * from comment where loginuid=? and seachuid=?',
		arr: [loginuid,searchuid],
		success(data) {
			res.send(data)
		},
		error(err) {
			res.send(err)
		}
	})
});

//小说点击率
router.post('/click',function(req,res){
	var json=req.body
	pool.mypool({
		arr:[json.num,json.uid],
		sql:'update search set numbers=? where uid=?',
		success(data) {
			res.send('ok')
		},
		error(err) {
			res.send(err)
		}
		
	})
})
//点击收藏
router.post('/csc',function(req,res){
	var json=req.body
	pool.mypool({
		arr:[json.user,json.img,json.uid,json.suid,json.simg,json.sname],
		sql:'insert into collect(user,userimg,loginuid,searchuid,searchimg,searchname) values(?,?,?,?,?,?)',
		success(data) {
			res.send('ok')
		},
		error(err) {
			res.send(err)
		}
		
	})
})
//我的收藏
router.post('/zsc',function(req,res){
	var json=req.body
	pool.mypool({
		arr:[json.uid],
		sql:'select * from collect where loginuid=?',
		success(data) {
			res.send(data)
		},
		error(err) {
			res.send(err)
		}
		
	})
})
//是否收藏
router.post('/cxsc',function(req,res){
	var json=req.body
	pool.mypool({
		arr:[json.uid,json.suid],
		sql:'select * from collect where loginuid=? and searchuid=?',
		success(data) {
			if (data.length) {
				res.send('已收藏')
			}else{
				res.send('收藏')
			}
		},
		error(err) {
			res.send(err)
		}
		
	})
})
module.exports = router;